package electricity.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import electricity.dto.WorkOrder.*;
import electricity.result.Result;
import electricity.service.WorkOrderService;
import electricity.vo.WorkOrder.WorkOrderDetailVO;
import electricity.vo.WorkOrder.WorkOrderListVO;
import electricity.vo.WorkOrder.WorkOrderStatusLogVO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/workorder")
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建工单")
    public Result<String> createWorkOrder(@RequestBody WorkOrderCreateDTO dto) {
        String workOrderId = workOrderService.createWorkOrder(
                dto.getFaultId(),
                dto.getDeviceId(),
                dto.getPriority()

        );
        return Result.success(workOrderId);
    }

    @PostMapping("/assign")
    @Operation(summary = "分配处理人")
    public Result<Boolean> assignProcessor(@RequestBody WorkOrderAssignDTO dto) {
        boolean result = workOrderService.assignProcessor(
                dto.getWorkOrderId(),
                dto.getProcessorId()

        );
        return Result.success(result);
    }

    @PostMapping("/start")
    @Operation(summary = "开始处理工单")
    public Result<Boolean> startProcessing(@RequestBody WorkOrderStartDTO dto) {
        boolean result = workOrderService.startProcessing(
                dto.getWorkOrderId(),
                StpUtil.getLoginIdAsString() // 当前登录用户ID
        );
        return Result.success(result);
    }

    @PostMapping("/complete")
    @Operation(summary = "完成工单")
    public Result<Boolean> completeWorkOrder(@RequestBody WorkOrderCompleteDTO dto) {
        boolean result = workOrderService.completeWorkOrder(
                dto.getWorkOrderId(),
                StpUtil.getLoginIdAsString(), // 当前登录用户ID
                dto.getNotes(),
                dto.getImages()
        );
        return Result.success(result);
    }

    @PostMapping("/fail")
    @Operation(summary = "标记工单失败")
    public Result<Boolean> markWorkOrderFailed(@RequestBody WorkOrderFailDTO dto) {
        boolean result = workOrderService.markWorkOrderFailed(
                dto.getWorkOrderId(),
                StpUtil.getLoginIdAsString(), // 当前登录用户ID
                dto.getFailureReason()
        );
        return Result.success(result);
    }

    @GetMapping("/detail")
    @Operation(summary = "查询工单详情")
    public Result<WorkOrderDetailVO> getWorkOrderDetail(@RequestParam String workOrderId) {
        WorkOrderDetailVO detail = workOrderService.getWorkOrderDetail(workOrderId);
        return Result.success(detail);
    }

    @GetMapping("/list")
    @Operation(summary = "查询工单列表")
    public Result<Page<WorkOrderListVO>> listWorkOrders(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer processorId
    ) {
        System.out.println("control接收的status参数: " + status); // 添加日志
        Page<WorkOrderListVO> page = workOrderService.listWorkOrders(status, pageNum, pageSize,processorId);
        return Result.success(page);
    }

    @GetMapping("/history")
    @Operation(summary = "查询工单状态变更历史")
    public Result<List<WorkOrderStatusLogVO>> getStatusHistory(@RequestParam String workOrderId) {
        List<WorkOrderStatusLogVO> history = workOrderService.getStatusHistory(workOrderId);
        return Result.success(history);
    }
}
